<?php

namespace Liuhelong\LaravelAdmin\Wechat\Http\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountCsMessage;
use \Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountNew;
use Log;
use Liuhelong\LaravelAdmin\Wechat\Common;

class WechatOffiaccountCsMessageController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '客服消息（用于自动回复）';
    protected $replay_types = [
		'text'=>'文字',
		'image'=>'图片',
		'voice'=>'语音',
		'video'=>'视频',
		'mpnewsarticle'=>'图文消息（公众号）',
		'news'=>'图文消息（自定义）',
		'miniprogrampage'=>'小程序卡片'
	];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatOffiaccountCsMessage());

        $grid->column('id', __('laravel_admin_wechat.Id'))->hide();
        $grid->column('name', __('laravel_admin_wechat.Name'));
        $grid->column('reply_type', __('laravel_admin_wechat.Reply type'))->using($this->replay_types);
        $grid->column('reply_content', __('laravel_admin_wechat.Reply content'))->display(function($reply_content,$column){
            
            return Common::getFile($this->reply_type,$reply_content);
        });;
        $grid->column('created_at', __('laravel_admin_wechat.Created at'));
        $grid->column('updated_at', __('laravel_admin_wechat.Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatOffiaccountCsMessage::findOrFail($id));

        $show->field('id', __('laravel_admin_wechat.Id'));
        $show->field('reply_type', __('laravel_admin_wechat.Reply type'));
        $show->field('reply_content', __('laravel_admin_wechat.Reply content'));
        $show->field('created_at', __('laravel_admin_wechat.Created at'));
        $show->field('updated_at', __('laravel_admin_wechat.Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatOffiaccountCsMessage());

		
        $form->text('name', __('laravel_admin_wechat.Name'));
        $form->select('reply_type', __('laravel_admin_wechat.Reply type'))->options($this->replay_types)
		->when('text', function (Form $form) { 

			$form->textarea('text', __('laravel_admin_wechat.Reply content'));

		})
		->when('in',['image','voice','video','mpnews'], function (Form $form) { 

			$form->text('reply_media', __('laravel_admin_wechat.Reply media'))->help('请输入素材库中相应素材的资源id');

		})
		->when('news', function (Form $form) { 

			$form->select('news', __('laravel_admin_wechat.Reply content'))->options(\Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountNew::all()->pluck('title','id')->all());

		})
		->when('miniprogrampage', function (Form $form) { 

            $form->embeds('miniprogrampage_data','小程序卡片数据', function ($form) {

                $form->text('title', __('laravel_admin_wechat.Reply title'));
    			$form->text('appid', __('laravel_admin_wechat.Reply appid'));
    			$form->text('pagepath', __('laravel_admin_wechat.Reply pagepath'));
    			$form->text('thumb_media_id', __('laravel_admin_wechat.Reply thumb media id'))->help('请输入素材库中相应素材的资源id');
            });

		});
		
        return $form;
    }
	
}
